package com.rykj.qxj.test.xtest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class HashMapKeySort {
	public static void main(String[] args) {
		
		Map<String, Integer> map = new HashMap<String, Integer>();  
        map.put("d", 2);  
        map.put("c", 1);  
        map.put("b", 4);  
        map.put("a", 3);  
        List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());  
  
        // 对HashMap中的key 进行排序  
        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {  
            public int compare(Map.Entry<String, Integer> o1,  
                    Map.Entry<String, Integer> o2) {  
                   System.out.println(o1.getKey()+"   ===  "+o2.getKey());  
                return (o1.getKey()).toString().compareTo(o2.getKey().toString());  
            }  
        });  
        // 对HashMap中的key 进行排序后  显示排序结果  
        for (int i = 0; i < infoIds.size(); i++) {  
            String id = infoIds.get(i).toString();  
            System.out.print(id + "  ");  
        }  
          
        System.out.println();  
        
        
        System.out.println("=====================================");
        
        List<Map<String,String>> resultMap=new ArrayList<Map<String,String>>();
        
        Map<String,String> timeMap=new HashMap<String,String>();
        timeMap.put("10:36", "6");
        timeMap.put("10:34", "4");
        timeMap.put("10:31", "1");
        timeMap.put("10:33", "3");
        timeMap.put("10:30", "0");
        timeMap.put("10:37", "7");
        timeMap.put("10:39", "9");
        timeMap.put("10:35", "5");
        List<Map.Entry<String, String>> timeInfo=new ArrayList<Map.Entry<String, String>>(timeMap.entrySet());
        Collections.sort(timeInfo, new Comparator<Map.Entry<String, String>>(){
			@Override
			public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
				return o1.getKey().toString().compareTo(o2.getKey().toString());
			}
        	
        });
        for (Entry<String, String> entry : timeInfo) {
        	Map<String,String> item=new HashMap<String,String>();
        	item.put("key", entry.getKey());
        	item.put("value", entry.getValue());
        	resultMap.add(item);
		}
        System.out.println(resultMap.toString());
          
	}

}
